package com.xiangyueEducation.uploaderCloud.service.impl;


import com.xiangyueEducation.uploaderCloud.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String myEmail;


    @Override
    public void sendSimpleEmail(String to, String subject, String text) {
        // 创建简单邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(myEmail); // 发送方邮箱
        message.setTo(to);                      // 接收方邮箱
        message.setSubject(subject);            // 邮件主题
        message.setText(text);                  // 邮件内容

        // 发送邮件
        mailSender.send(message);
    }

    @Override
    public void sendCaptcha(String to, String captcha) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(myEmail);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject("欢迎使用心晴APP, 请接收验证码");
        simpleMailMessage.setText("您的验证码为: " + captcha + "，请勿泄露给他人(5分钟内有效).");
        mailSender.send(simpleMailMessage);
    }
}
